<template>
    <div class="app-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['medicine:order:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            :disabled="multiple"
            @click="handleAdd()"
            v-hasPermi="['medicine:order:remove']"
          >生成订单</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="carList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单ID" align="center" prop="drugId" />
        <el-table-column label="药品名称" align="center" prop="drugName" />
        <el-table-column label="厂家" align="center" prop="factoryId" />
        <el-table-column label="采购人" align="center" prop="createBy" />
        <el-table-column label="药品数量" align="center" prop="carNum">
            <template #default="scope">
                <el-input-number v-model="scope.row.carNum" :value="scope.row.carNum" :min="1" @change="handleChange(scope.row)" />
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['medicine:order:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供应商" prop="orderStatus">
          <el-select v-model="form.supplierId" placeholder="请选择供应商">
            <el-option
              v-for="dict in supplier_list"
              :key="dict"
              :label="dict.supplierName"
              :value="dict.supplierId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </template>
  
  <script setup name="Order">
  import { listCar , updateCar , deleteCar , addCar , listSupplier} from "@/api/his/orderCar.js";
  const { proxy } = getCurrentInstance();
  const router = useRouter();
  const carList = ref("");
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const open = ref(false);
  const single = ref(true);
  const multiple = ref(true);
  
  const data = reactive({
    form: {
      drugId:undefined,
      supplierId:undefined,
    },
    rules: {
      orderPerson: [
        { required: true, message: "制单人不能为空", trigger: "change" }
      ],
      supplierId: [
        { required: true, message: "供应商ID不能为空", trigger: "change" }
      ],
    }
  });
  
  const {form,rules} = toRefs(data);
  
  /** 查询订单信息列表 */
  function getList() {
    loading.value = true;
    listCar().then(response => {
      carList.value = response.data;
      loading.value = false;
    });
  }

  

  // 表单重置
function reset() {
  form.value = {
    durgId: null,
    supplierId:null,
    remark: null
  };
  proxy.resetForm("orderRef");
 }

  // 取消按钮
 function cancel() {
    open.value = false;
    reset();
 }
  // 添加订单
  function submitForm(){
    form.value.durgId = ids.value;//药品id
    addCar(form.value).then(res=>{
      proxy.$modal.msgSuccess("添加成功");
      open.value = false;
      getList();
   })
  }

  // 打开添加供应人窗口
  function  handleAdd(){
     reset();
     open.value = true;
     
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.drugId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  //修改药品数量    
  function handleChange(data){
    updateCar(data.drugId,data.carNum)
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _orderIds = row.drugId || ids.value;
    proxy.$modal.confirm('是否确认删除订单信息编号为"' + _orderIds + '"的药品数据吗？').then(function() {
      return deleteCar(_orderIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  const supplier_list = ref(0);
  listSupplier().then(res=>{
    supplier_list.value = res.rows
  });
  getList();
  </script>
  